package com.xm.linxiaogang.base.application

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Process
import androidx.multidex.MultiDex
import com.alibaba.android.arouter.launcher.ARouter
import com.xm.linxiaogang.base.activity.swipeback.ActivityLifecycleHelper
import com.xm.linxiaogang.utils.ToastUtils
import com.zxy.tiny.Tiny

class LibBaseApplication : Application() {

    companion object {
        lateinit var context: Context
    }

    init {
        context = this
    }

    override fun onCreate() {
        super.onCreate()

        //初始化路由
        ARouter.init(this)

        //初始化Toast
        ToastUtils.init(this)

        //初始化Tiny图片压缩
        Tiny.getInstance().init(this)

        //监听所有Activity的生命周期
        registerActivityLifecycleCallbacks(ActivityLifecycleHelper.build())
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        //分包
        MultiDex.install(this)
    }

    /**
     * 获取进程名称
     */
    fun getProcessName(context: Context): String? {
        val manager = context.getSystemService(ACTIVITY_SERVICE) as ActivityManager
        for (processInfo in manager.runningAppProcesses) {
            if (processInfo.pid == Process.myPid()) {
                return processInfo.processName
            }
        }
        return null
    }

    //判断Activity是否处于栈顶
    fun isActivityTop(cls: Class<*>, context: Context): Boolean {
        val manager = context.getSystemService(ACTIVITY_SERVICE) as ActivityManager
        val name = manager.getRunningTasks(1)[0].topActivity!!.className
        return name == cls.name
    }
}
